'''
# LOG算子
[1] https://www.mdeditor.tw/pl/p1RS/zh-hk
'''
import numpy as np
import cv2
import matplotlib.pyplot as plt
import math

# 用Sobel,Roberts,Prewitt,LOG,Canny算子进行边缘检测

def sobelFilter(img):
	# Sobel 算子
	x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
	y = cv2.Sobel(img, cv2.CV_16S, 0, 1)

	# 转 uint8 ,图像融合
	absX = cv2.convertScaleAbs(x)
	absY = cv2.convertScaleAbs(y)

	# addWeighted(src1, alpha, src2, beta, 0.0)
	Sobel_img = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)


	# plt.subplot(121),plt.imshow(img, cmap = 'gray')
	# plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	# plt.subplot(122),plt.imshow(Sobel_img, cmap = 'gray')
	# plt.title('Sobel Image'), plt.xticks([]), plt.yticks([])
	# plt.show()

	return Sobel_img


def RobertsFilter(img):
	# Roberts 算子
	kernelx = np.array([[-1, 0], [0, 1]], dtype=int)
	kernely = np.array([[0, -1], [1, 0]], dtype=int)

	x = cv2.filter2D(img, cv2.CV_16S, kernelx)
	y = cv2.filter2D(img, cv2.CV_16S, kernely)
	# 转 uint8 ,图像融合
	absX = cv2.convertScaleAbs(x)
	absY = cv2.convertScaleAbs(y)
	Roberts_img = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)

	# plt.subplot(121),plt.imshow(img, cmap = 'gray')
	# plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	# plt.subplot(122),plt.imshow(Roberts_img, cmap = 'gray')
	# plt.title('Roberts Image'), plt.xticks([]), plt.yticks([])
	# plt.show()

	return Roberts_img


def prewittFilter(img):
	# Prewitt 算子
	kernelx = np.array([[1,1,1],[0,0,0],[-1,-1,-1]],dtype=int)
	kernely = np.array([[-1,0,1],[-1,0,1],[-1,0,1]],dtype=int)

	x = cv2.filter2D(img, cv2.CV_16S, kernelx)
	y = cv2.filter2D(img, cv2.CV_16S, kernely)

	# 转 uint8 ,图像融合
	absX = cv2.convertScaleAbs(x)
	absY = cv2.convertScaleAbs(y)
	Prewitt_img = cv2.addWeighted(absX, 0.5, absY, 0.5, 0)

	# plt.subplot(121),plt.imshow(img, cmap = 'gray')
	# plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	# plt.subplot(122),plt.imshow(Prewitt_img, cmap = 'gray')
	# plt.title('Prewitt Image'), plt.xticks([]), plt.yticks([])
	# plt.show()

	return Prewitt_img


def LaplacianFilter(img):
	# Laplacian
	dst = cv2.Laplacian(img, cv2.CV_16S, ksize = 3)
	Laplacian_img = cv2.convertScaleAbs(dst)

	plt.subplot(121),plt.imshow(img, cmap = 'gray')
	plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(122),plt.imshow(Laplacian_img, cmap = 'gray')
	plt.title('Prewitt Image'), plt.xticks([]), plt.yticks([])
	plt.show()

	return Laplacian_img


def logFilter(img):
	kernel = np.array([[-2,-4,-4,-4,-2],
					   [-4, 0, 8, 0,-4],
					   [-4, 8, 24,8,-4],
					   [-4, 0, 8, 0,-4],
					   [-2,-4,-4,-4,-2]], dtype=int)

	log_img = cv2.filter2D(img, cv2.CV_16S, kernel)
	abs_img = cv2.convertScaleAbs(log_img)

	# plt.subplot(121),plt.imshow(img, cmap = 'gray')
	# plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	# plt.subplot(122),plt.imshow(abs_img, cmap = 'gray')
	# plt.title('LOG Image'), plt.xticks([]), plt.yticks([])
	# plt.show()

	return abs_img


def cannyFilter(img):
	edges = cv2.Canny(img,100,200)

	return edges



if __name__ == '__main__':
	img = cv2.imread("../images/Lenna.png", 0)
	sobel_img = sobelFilter(img)
	roberts_img = RobertsFilter(img)
	prewitt_img = prewittFilter(img)
	log_img = logFilter(img)
	canny_img = cannyFilter(img)
	

	plt.subplot(231),plt.imshow(img, cmap = 'gray')
	plt.title('Input Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(232),plt.imshow(sobel_img, cmap = 'gray')
	plt.title('Sobel Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(233),plt.imshow(roberts_img, cmap = 'gray')
	plt.title('Roberts Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(234),plt.imshow(prewitt_img, cmap = 'gray')
	plt.title('Prewitt Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(235),plt.imshow(log_img, cmap = 'gray')
	plt.title('LOG Image'), plt.xticks([]), plt.yticks([])
	plt.subplot(236),plt.imshow(canny_img, cmap = 'gray')
	plt.title('Canny Image'), plt.xticks([]), plt.yticks([])
	plt.show()
	

